package charpter01_basic.lession05_impl_producter_consumer;

import charpter01_basic.lession05_impl_producter_consumer.my_queue.MyBlockingQueueForCondition;

/**
 * 使用LockCondition实现阻塞队列，然后实现生产者消费者模型
 */
public class ProducterConsumer2 {
    private static final MyBlockingQueueForCondition MY_BLOCKING_QUEUE_FOR_CONDITION = new MyBlockingQueueForCondition(16);

    public static void main(String[] args) {
        new Thread(() -> {
            while (true) {
                try {
                    MY_BLOCKING_QUEUE_FOR_CONDITION.put(1);
                    System.out.println("加入 1 还有 " + MY_BLOCKING_QUEUE_FOR_CONDITION.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


        new Thread(() -> {
            while (true) {
                Object take = null;
                try {
                    take = MY_BLOCKING_QUEUE_FOR_CONDITION.take();
                    System.out.println("取出 " + take + " 还有 " + MY_BLOCKING_QUEUE_FOR_CONDITION.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}
